/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-6-14 下午3:53
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dqhl.jdmetro.JDMetroApplication;
import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.bean.User;
import com.example.dqhl.jdmetro.utils.UserSaveUtil;
import com.example.dqhl.jdmetro.widget.CustomProgressDialog;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.app.TakePhotoImpl;
import com.jph.takephoto.model.InvokeParam;
import com.jph.takephoto.model.TContextWrap;
import com.jph.takephoto.model.TResult;
import com.jph.takephoto.permission.InvokeListener;
import com.jph.takephoto.permission.PermissionManager;
import com.jph.takephoto.permission.TakePhotoInvocationHandler;

/**
 * description:BaseFragment
 * autour:guanluocang
 * date:17/6/14 15:54
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener,TakePhoto.TakeResultListener,InvokeListener {

    private Context context;
    private ImageView iv_top_back;
    private TextView tv_top_title;
    private TextView tv_top_msg;
    protected JDMetroApplication app;
    private CustomProgressDialog mCustomProgressDialog;
    protected LocalBroadcastManager localBroadcastManager;

    public View rootView;
    public Layout view;
    protected View mView;
    protected User user;
    private static final String TAG = TakePhotoActivity.class.getName();
    private TakePhoto takePhoto;
    private InvokeParam invokeParam;
    /**
     * 表示View是否被初始化
     */
    protected boolean isViewInitiated;
    /**
     * 表示对用户是否可见
     */
    protected boolean mIsVisibleToUser;
    /**
     * 表示数据是否初始化
     */
    protected boolean isDataInitiated;

    @Override
    public void onActivityCreated( Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewInitiated = true;
        initData();
        initListener();
        prepareFetchData();
        getTakePhoto().onCreate(savedInstanceState);
    }

    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        app = (JDMetroApplication) getActivity().getApplicationContext();
        user = (User) UserSaveUtil.readUser(context);
        localBroadcastManager = LocalBroadcastManager.getInstance(getContext());


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = initView(inflater);
        return rootView;
    }
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
//        LUtils.i(this.getClass().getSimpleName()+"<<<<<<<<<<<<   setUserVisibleHint"+isVisibleToUser);
        super.setUserVisibleHint(isVisibleToUser);
        this.mIsVisibleToUser = isVisibleToUser;
        prepareFetchData();
    }


    public boolean prepareFetchData() {
        return prepareFetchData(false);
    }

    public boolean prepareFetchData(boolean forceUpdate) {
        if (mIsVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
            isDataInitiated = true;
            return true;
        }
        return false;
    }
    public abstract View initView(LayoutInflater inflater);

    public abstract void initData();

    public abstract void initListener();

    public void initTitleBar(View view, String title) {
        iv_top_back = (ImageView) view.findViewById(R.id.iv_top_back);
        iv_top_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        tv_top_title = (TextView) view.findViewById(R.id.tv_top_title);
        tv_top_title.setText(title);
        tv_top_msg = (TextView) view.findViewById(R.id.tv_top_msg);
    }


    public void initTitleBar(View view, int resId) {
        String title = getResources().getString(resId);
        iv_top_back = (ImageView) view.findViewById(R.id.iv_top_back);
        iv_top_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        tv_top_title = (TextView) view.findViewById(R.id.tv_top_title);
        tv_top_title.setText(title);
        tv_top_msg = (TextView) view.findViewById(R.id.tv_top_msg);
    }

    public void setLeftBackVisibility(View view, int visible) {
        if (iv_top_back != null) {
            iv_top_back.setVisibility(visible);
        } else {
            iv_top_back = (ImageView) view.findViewById(R.id.iv_top_back);
            iv_top_back.setVisibility(visible);
        }
    }

    public void setRightTextVisibility(View view, int visible) {
        if (tv_top_msg != null) {
            tv_top_msg.setVisibility(visible);
        } else {
            tv_top_msg = (TextView) view.findViewById(R.id.tv_top_msg);
            tv_top_msg.setVisibility(visible);
        }
    }

    public void setRightTextListener(View view, View.OnClickListener listener) {
        if (tv_top_msg != null) {
            tv_top_msg.setOnClickListener(listener);
        } else {
            tv_top_msg = (TextView) view.findViewById(R.id.tv_top_msg);
            tv_top_msg.setOnClickListener(listener);
        }
    }

    /**
     * 显示自定义ProgressBar，提示用户操作正在处理中
     *
     * @param content:要显示的提示内容包括ProgressBar和text
     */
    public void showLoadingBar(String content) {
        if (mCustomProgressDialog == null) {
            mCustomProgressDialog = new CustomProgressDialog(getActivity(), content);
        } else {
            mCustomProgressDialog.setContent(content);
        }
        mCustomProgressDialog.show();
    }

    /**
     * 释放自定义ProgressBar，提示操作结果
     */
    public void hideLoadingBar() {
        if (mCustomProgressDialog != null && mCustomProgressDialog.isShowing()) {
            mCustomProgressDialog.cancel();
        }
    }

    @Override
    public void onClick(View v) {

    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        getTakePhoto().onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }  @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        getTakePhoto().onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.TPermissionType type=PermissionManager.onRequestPermissionsResult(requestCode,permissions,grantResults);
        PermissionManager.handlePermissionsResult(getActivity(),type,invokeParam,this);
    }

    /**
     *  获取TakePhoto实例
     * @return
     */
    public TakePhoto getTakePhoto(){
        if (takePhoto==null){
            takePhoto= (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this,this));
        }
        return takePhoto;
    }
    @Override
    public void takeSuccess(TResult result) {
        Log.i(TAG,"takeSuccess：" + result.getImage().getCompressPath());
    }
    @Override
    public void takeFail(TResult result,String msg) {
        Log.i(TAG, "takeFail:" + msg);
    }
    @Override
    public void takeCancel() {
        Log.i(TAG, getResources().getString(com.jph.takephoto.R.string.msg_operation_canceled));
    }

    @Override
    public PermissionManager.TPermissionType invoke(InvokeParam invokeParam) {
        PermissionManager.TPermissionType type=PermissionManager.checkPermission(TContextWrap.of(this),invokeParam.getMethod());
        if(PermissionManager.TPermissionType.WAIT.equals(type)){
            this.invokeParam=invokeParam;
        }
        return type;
    }
}
